package com.bianguo.android.tencentpushdemo.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.adapters.CommentAdapter;
import com.bianguo.android.tencentpushdemo.utils.MyLog;
import com.bianguo.android.tencentpushdemo.view.widget.CircleImageView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class EditRaidersActivity extends BaseActivity implements CommentAdapter.OnClickItemListener {

    @BindView(R.id.edit_raiders_bg)
    ImageView bgIv;
    @BindView(R.id.edit_raiders_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.edit_raiders_icon)
    CircleImageView iconIv;

    public static final String TEST_IMAGE_URL = "http://sc.bianguo.com.cn/scnews/Uploads/avatar/2017-06-21/594a333991c84.jpg";
    private CommentAdapter commentAdapter;
    private ArrayList<String> raiders;

    @Override
    public int layoutId() {
        return R.layout.activity_edit_raiders;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        Glide.with(this)
                .load(TEST_IMAGE_URL)
                .into(iconIv);
        raiders = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            raiders.add("编辑攻略 1");
        }
        commentAdapter = new CommentAdapter(this, raiders);
        mRecycler.setAdapter(commentAdapter);
        commentAdapter.setOnClickItemListener(this);
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.edit_raiders_icon})
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.edit_raiders_icon:
                int borderColor = iconIv.getBorderColor();
                if (ActivityCompat.getColor(this, R.color.colorImageBorder) == borderColor) {
                    iconIv.setBorderColor(Color.TRANSPARENT);
                } else {
                    iconIv.setBorderColor(ActivityCompat.getColor(this, R.color.colorImageBorder));
                }
//                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
//                intent.setData(Uri.parse("package:" + getPackageName()));
//                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.System.canWrite(this)) {
                }
            }
        }
    }

    @Override
    public void onClickItem(View view, int position) {
        switch (view.getId()) {
            case R.id.item_add_raiders:
                raiders.add(position, "onClickItem " + position);
                commentAdapter.notifyItemInserted(position);
                break;
            case R.id.item_home_add_raiders:
                if (position == raiders.size()) {
                    raiders.add("onClickItem " + position);
                    commentAdapter.notifyItemInserted(position + 1);
                } else {
                    raiders.add(position, "onClickItem " + position);
                    commentAdapter.notifyItemInserted(position + 1);
                }
                break;
            case R.id.item_home_text:
                raiders.remove(position - 1);
                commentAdapter.notifyItemRemoved(position);
                break;
            case R.id.item_home_image:
                Toast.makeText(mContext, raiders.get(position - 1), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
